import processGPR as gpr
import numpy as np
import os
from pylab import scatter, show
from gprViewerUIs import wxOpenFile
   

def convertCband():
    ''' Converts raw C-band data to time domain and stores it as '.h5' files '''
    procdir = 'Y:\Data2008_09\Cband\0301'
    os.chdir(procdir)    
    filelist = os.listdir(os.getcwd())
    filelist.sort()
    print filelist
    for file in filelist:
        if file[-4:] == '.gYY':
            FD = gpr.CFD()
            FD.readRaw(file)
            profile = gpr.CTD()
            profile.timeDomain(FD.data)
            profile.hascoords = FD.hascoords
            profile.coords = FD.coords
            ''' Uncomment for static correction '''
#            profile.staticCorrection(258)
            profile.staticCorrection()
            ''' Uncomment for calculating weighted scattering cross section '''
#            profile.wscs()
            if len(file[7:-4]) == 1:
                profile.filename = os.path.split(os.getcwd())[-1]+'_YY0'+file[7:-4]
            else:
                profile.filename = os.path.split(os.getcwd())[-1]+'_YY'+file[7:-4]
            profile.writeH5(profile.filename)

def convertCbandNew():
    ''' Converts raw C-band data to time domain and stores it as '.h5' files '''
    procdir = r'Y:\Data2008_09\Cband\0201'
    file = '20090102'
    os.chdir(procdir) 
    fout = os.path.join(procdir, file)
    pro = gpr.CBAND()
    pro.convertHDF(fout, combine=True, filerange="2-4")
    
       
#    filelist = os.listdir(os.getcwd())
#    filelist.sort()
#    print filelist
#    for file in filelist:
#        if file[-4:] == '.gYY':
#            pro = gpr.CBAND()
#            pro.readRaw(file)
#            pro.applyWindow()
#            pro.zeropad()
#            pro.applyFFT()
#            profile = gpr.CTD()
#            profile.timeDomain(FD.data)
#            profile.hascoords = FD.hascoords
#            profile.coords = FD.coords
#            ''' Uncomment for static correction '''
##            profile.staticCorrection(258)
#            profile.staticCorrection()
#            ''' Uncomment for calculating weighted scattering cross section '''
##            profile.wscs()
#            if len(file[7:-4]) == 1:
#                profile.filename = os.path.split(os.getcwd())[-1]+'_YY0'+file[7:-4]
#            else:
#                profile.filename = os.path.split(os.getcwd())[-1]+'_YY'+file[7:-4]
#            profile.writeH5(profile.filename)
    
def convertHoltedahl():
    wdir = r'/mn/terra/hubradata/Hubra_Svalbard/Holtedahlfonna_2009'
    file = 'data52'
    pro = gpr.HUBRA()
    pro.numfreq = 5000
#    pro.Nfft = 2**14
    pro.convertHDF(os.path.join(wdir, file), wdir,
                     999, combine=False, stack=1, filerange="all-all") 

    pro.addCoords(os.path.join(wdir, file+'.h5'), os.path.join(wdir, file+'.cor'))
    scatter(pro.coords.x, pro.coords.y, c=pro.coords.elev, edgecolors='none')  
    show()
    
    
def convertTraverse():
    wdir = r'/mn/terra/hubradata/Data2008_09/Wisdom/2501'
#    wdir = r"Y:\Data2008_09\Wisdom\2312"
    file = 'data0'
    pro = gpr.HUBRA()

    pro.convertHDF(os.path.join(wdir, file), wdir,
                     1000, combine=True, stack=25, filerange="all-all",
                     ww=0, cf=0) 
                    # ww is the filter-window width and cf the window-center-frequency if the spectrum should be clipped
    pro.addCoords(os.path.join(wdir, file+'.h5'), os.path.join(wdir, file+'.cor'))
    scatter(pro.coords.x, pro.coords.y, c=pro.coords.elev, edgecolors='none')  
    show()


def convertKongsvegen08():
    wdir = r'/mn/terra/hubradata/Hubra_Svalbard/Kongsvegen_2008'
    file = 'data Thu Apr 24 13-15-42 2008 gated  sweep 1 MHz0'
    pro = gpr.HUBRA()
    pro.gatefreq = 1523123
    pro.convertHDF(os.path.join(wdir, file), wdir,
                     1000, combine=True, stack=25, filerange="all-all") 

    pro.addCoords(os.path.join(wdir, file+'.h5'), os.path.join(wdir, file+'.cor'), projection='UTM')
    scatter(pro.coords.x, pro.coords.y, c=pro.coords.elev, edgecolors='none')  
    show()
 
 
def convertFimbul():
    wdir = r'/mn/terra/hubradata/fimbuldata/HUBRAdata/091120'
#    wdir = r"Y:\fimbuldata\HUBRAdata\091120"
    file = 'data345'
    pro = gpr.FIMBUL()

    pro.convertHDF(os.path.join(wdir, file), wdir,
                     13000, combine=True, stack=25, filerange="all-all") 

    pro.addCoords(os.path.join(wdir, file+'.h5'), os.path.join(wdir, file+'.cor'))
    scatter(pro.coords.x, pro.coords.y, c=pro.coords.elev, edgecolors='none')  
    show()

 
def merge():
    ''' First step is to set the directory containing all CONVERTED files that should be merged '''
    procdir = 'Y:/Data2007_08/CBAND/scambos'
    os.chdir(procdir)
#    filelist = os.listdir(os.getcwd())
    filelist = wxOpenFile(procdir)
    filelist.sort()
    print filelist
    
    section = gpr.CTD()
    profile = gpr.CTD()
    parts = []; px = []; py = []; pelev = []
    for file in filelist:
        print 'Adding', file, '...'
        profile.readH5(file)
#        profile.staticCorrection()
#        profile.stackTraces(20)
        profile.distanceStack(2.0)
#        profile.wscs()
        parts.append(profile.data)
        py.append(profile.coords.y)
        px.append(profile.coords.x)
        pelev.append(profile.coords.elev)
    section.data = np.concatenate(parts, axis=1)
    section.coords = gpr.COORDS(section.traces)
    section.hascoords = True
    section.coords.y = np.concatenate(py, axis=0)
    section.coords.x = np.concatenate(px, axis=0)
    section.coords.elev = np.concatenate(pelev, axis=0)
    section.coords.getdistance()
#    section.stackTraces(2)
    section.filename = '%s to %s' % (filelist[0], filelist[-1])
    sname = 'scambos_along' 
    section.writeH5(sname)
    
   
if __name__ == '__main__':
    ''' Uncomment the function you want to run and comment the other,
    then just excute the script by double-clicking on processing.py '''
#    convertCbandNew()    
#    convertKongsvegen08()
#    convertFimbul()
    convertTraverse()
#    merge()